﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevComponents.DotNetBar;
using System.Collections;
using ESRI.ArcGIS.esriSystem;
using ESRI.ArcGIS.Display;
using PlanningGIS.Win.Controls;
using ESRI.ArcGIS.Controls;
using PlanningGIS.Util.Management;
using PlanningGIS.ArcGIS;

namespace PlanningGIS.Symbol
{
    /// <summary>
    /// 点符号界面
    /// </summary>
    public partial class FrmMarkerStyle : OfficeForm
    {
        /// <summary>
        /// 构造函数
        /// </summary>
        public FrmMarkerStyle()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 加载窗体
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void FrmMarkerStyle_Load(object sender, EventArgs e)
        {
            InitListView();
            dropDownStyle.SelectedChangedEvent += new EventHandler(dropDownStyle_SelectedChangedEvent);
        }

        /// <summary>
        /// 
        /// </summary>
        ArrayList itemArray;
        /// <summary>
        /// 
        /// </summary>
        ArrayList pArraylist;
        /// <summary>
        /// 
        /// </summary>
        int CurIndex;
        /// <summary>
        /// 选择的符号
        /// </summary>
        private ISymbol selSymbol;
        /// <summary>
        /// 
        /// </summary>
        private ISymbol bakSymbol;
        /// <summary>
        /// 当前选择的符号
        /// </summary>
        public ISymbol SelectedSymbol
        {
            set
            {
                selSymbol = value;
                if (selSymbol == null) return;
                IClone pClone = (IClone)selSymbol;
                bakSymbol = (ISymbol)pClone.Clone();
            }
            get
            {
                return selSymbol;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        void dropDownStyle_SelectedChangedEvent(object sender, EventArgs e)
        {
            InitListView();
            cbxCategory.Text = "全部";
            cbxCategory_SelectedIndexChanged(null, null);
        }
        /// <summary>
        /// 初始化ListView
        /// </summary>
        private void InitListView()
        {
            //this.Cursor = Cursors.WaitCursor;
            //List<string> categoryList = SymbolHelper.GetCategories(PlanningGIS.ArcGIS.zjgisStyleGalleryType.MarkerSymbols);
            //this.cbxCategory.Items.Clear();
            //this.cbxCategory.Items.Add("全部");
            //cbxCategory.Items.AddRange(categoryList.ToArray());
            ////for (int i = 0; i < categoryList.Count; i++)
            ////{
            ////    this.cbxCategory.Items.Add(categoryList[i].ToString());
            ////}
            ////this.cbxCategory.SelectedIndexChanged += new EventHandler(cbxCategory_SelectedIndexChanged);
            //this.cbxCategory.SelectedIndex = 0;
            //this.Cursor = Cursors.Default;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void cbxCategory_SelectedIndexChanged(object sender, EventArgs e)
        {
            string pCategory = "";
            if (cbxCategory.Text == "全部")
            {
                pCategory = null;
            }
            else
            {
                pCategory = cbxCategory.Text;
            }
            itemArray = new ArrayList();
            //pArraylist = AoHelper.GetCurrentStyle();
            this.imageList1.Images.Clear();
            this.listView.Items.Clear();
            DrawSymbolPreview(true);
            //SymbolHelper.GetStyles(PlanningGIS.ArcGIS.zjgisStyleGalleryType.MarkerSymbols, itemArray, pArraylist, this.imageList1, this.listView, pCategory);
            this.listView.View = View.LargeIcon;
            this.listView.SelectedIndexChanged += new EventHandler(listView_SelectedIndexChanged);
        }

        /// <summary>
        /// 
        /// </summary>
        private void ClearItems()
        {
            itemArray.Clear();
            this.listView.Items.Clear();
            this.imageList1.Images.Clear();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void listView_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.listView.SelectedItems.Count == 0) return;
            try
            {
                CurIndex = this.listView.SelectedItems[0].Index;
                selSymbol = (ISymbol)itemArray[CurIndex];
                IClone pClone = (IClone)selSymbol;
                bakSymbol = (ISymbol)pClone.Clone();
                DrawSymbolPreview(true);
            }
            catch
            {
               
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="p_IsSelectDraw"></param>
        private void DrawSymbolPreview(bool p_IsSelectDraw)
        {
            try
            {
                if (this.selSymbol != null)
                {
                    this.pictureBox1.Refresh();
                    IMarkerSymbol symbol3 = (IMarkerSymbol)this.selSymbol;
                    colorCreator1.SelectedColor = PlanningGIS.ArcGIS.Convertor.IColor2Color(symbol3.Color);
                    if (p_IsSelectDraw)
                    {
                        txtSize.Value = symbol3.Size;
                        txtAngle.Value = symbol3.Angle;
                    }
                }
            }
            catch (Exception ex)
            {
                MsgHelper.ShowErrorMsgbox(ex.Message);
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void pictureBox1_Paint(object sender, PaintEventArgs e)
        {
            if (this.selSymbol != null)
            {
                //SymbolHelper.DrawSymbol(e.Graphics, this.pictureBox1.BackColor, selSymbol, 0, 0, this.pictureBox1.Width, this.pictureBox1.Height, 1, false);
            }
        }
        /// <summary>
        /// 设置颜色
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="color"></param>
        private void btnMarkerColor_SelectedChanged(object sender, Color color)
        {
            if (selSymbol != null)
            {
                IMarkerSymbol symbol1 = (IMarkerSymbol)this.selSymbol;
                //SymbolHelper.EditMarkerSymbol(symbol1, btnMarkerColor.SelectedColor, symbol1.Size, symbol1.Angle);

                symbol1.Color = Convertor.Color2IColor(colorCreator1.SelectedColor);
                //symbol1.Size = symbol1.Size;
                //symbol1.Angle = symbol1.Angle;

                this.DrawSymbolPreview(false);
            }
        }
        
        /// <summary>
        /// 设置大小
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void txtSize_ValueChanged(object sender, EventArgs e)
        {
            if (this.selSymbol != null)
            {
                IMarkerSymbol symbol1 = (IMarkerSymbol)this.selSymbol;
                //SymbolHelper.EditMarkerSymbol(symbol1, symbol1.Color, txtSize.Value, symbol1.Angle);                
                symbol1.Size = txtSize.Value;
                //symbol1.Angle = symbol1.Angle;
                //symbol1.Color = symbol1.Color;
                this.DrawSymbolPreview(false);
            }
        }

        /// <summary>
        /// 设置角度
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void txtAngle_ValueChanged(object sender, EventArgs e)
        {
            if (this.selSymbol != null)
            {
                IMarkerSymbol symbol1 = (IMarkerSymbol)this.selSymbol;
                //SymbolHelper.EditMarkerSymbol(symbol1, symbol1.Color, symbol1.Size, txtAngle.Value);
                //symbol1.Color = symbol1.Color;
                //symbol1.Size = symbol1.Size;
                symbol1.Angle = txtAngle.Value;
                this.DrawSymbolPreview(false);
            }
        }

        /// <summary>
        /// 重置
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnReset_Click(object sender, EventArgs e)
        {
            if (this.bakSymbol == null) return;
            IClone pClone = (IClone)bakSymbol;
            this.selSymbol = (ISymbol)pClone.Clone();
            if (CurIndex > 0)
                itemArray[CurIndex] = selSymbol;
            this.DrawSymbolPreview(true);
        }

        /// <summary>
        /// 确定
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void BtnOk_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.OK;
        }

        /// <summary>
        /// 取消
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void BtnClose_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
        }

        
    }
}
